﻿using System;
using System.Configuration;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;

namespace TMS
{
    public partial class UsersControl : UserControl
    {
        private bool listOfUsersLoaded = false;
        public MySqlDataAdapter MyDA = new MySqlDataAdapter();

        public UsersControl()
        {
            InitializeComponent();
            dgvUsersList.AutoGenerateColumns = false;


            if (!listOfUsersLoaded)
            {
                loadListOfUsers();
                listOfUsersLoaded = true;
            }
        }

        private void loadListOfUsers()
        {
            string connString = ConfigurationManager.ConnectionStrings["MyDBConnectionString"].ConnectionString;
            string[] row;

            MySqlConnection mysqlCon = new MySqlConnection(connString);
            mysqlCon.Open();


            string sqlSelectAll = "SELECT * from Users";
            MyDA.SelectCommand = new MySqlCommand(sqlSelectAll, mysqlCon);

            //star kod
            /*DataTable table = new DataTable();
            MyDA.Fill(table);

            BindingSource bSource = new BindingSource();
            bSource.DataSource = table;

            dgvUsersList.DataSource = bSource;
            mysqlCon.Close();*/


            //nov kod
            DataSet table = new DataSet();
            MyDA.Fill(table);

            dgvUsersList.ColumnCount = 5;
            dgvUsersList.Columns[0].Name = "ID";
            dgvUsersList.Columns[0].Visible = false;
            dgvUsersList.Columns[1].Name = "Име";
            dgvUsersList.Columns[2].Name = "Презиме";
            dgvUsersList.Columns[3].Name = "Корисничко име";
            dgvUsersList.Columns[4].Name = "Лозинка";
            
            foreach (DataRow User in table.Tables[0].Rows)
            {
                row = new string[] { User[0].ToString(),User[1].ToString(), User[2].ToString(), User[3].ToString(), User[4].ToString() };
                dgvUsersList.Rows.Add(row);
            }

            mysqlCon.Close();

        }

        private void btnNewUser_Click(object sender, EventArgs e)
        {
            Program.GetMainForm.ShowControl(ControlsEnum.CREATE_EDIT_USER_CONTROL);
        }

        private void btnDeleteUser_Click(object sender, EventArgs e)
        {
            string todelete = dgvUsersList.SelectedRows[0].Cells[3].Value.ToString();
            string sqlDelete = "DELETE FROM Users WHERE Username='" + todelete + "'";

            string connString = ConfigurationManager.ConnectionStrings["MyDBConnectionString"].ConnectionString;

            MySqlConnection conn = null;
            conn = new MySqlConnection(connString);
            conn.Open();
            MySqlCommand cmd = new MySqlCommand(sqlDelete, conn);
            cmd.ExecuteNonQuery();
            conn.Close();

            //UsersDataGridView.Update();

            loadListOfUsers();
        }

        private void btnRefreshUsers_Click(object sender, EventArgs e)
        {
            loadListOfUsers();
        }

        private void btnProjects_Click(object sender, EventArgs e)
        {
            this.Hide();
            Program.GetMainForm.ShowControl(ControlsEnum.PROJECTS_CONTROL);
        }

        private void btnEmployees_Click(object sender, EventArgs e)
        {
            this.Hide();
            Program.GetMainForm.ShowControl(ControlsEnum.EMPLOYEES_CONTROL);
        }

        private void btnProducts_Click(object sender, EventArgs e)
        {
            this.Hide();
            Program.GetMainForm.ShowControl(ControlsEnum.PRODUCTS_CONTROL);
        }

        private void dgvUsersList_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            DataGridViewRow row = dgvUsersList.CurrentRow;
            Program.GetMainForm.ShowControl(ControlsEnum.CREATE_EDIT_USER_CONTROL, row);
            this.Dispose();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Product prod = new Product();
            prod.CorectWorkOrderProductPrices();
        }

        private void ToStBack_Click(object sender, EventArgs e)
        {
            if (Program.GetMainForm.Back())
                this.Dispose();
        }


        private void ToStForward_Click(object sender, EventArgs e)
        {
            if (Program.GetMainForm.Forward())
                this.Dispose();
        }

    }
}
